/* *************************************************************************** *
* Sierra Leone - Social Incentives for Childhood Immunization                  *
* Effects of Signals on Timely Vaccination - Non-Absorbing State               *
* **************************************************************************** *

** Purpose:     Effects of Signals on Timely Vaccination - Non-Absorbing State

*/
* **************************************************************************** *


	use  "${Replicate_SocialSignals_dtaInter}/ListingFollowUp_analysis.dta", clear


	* Keep only children that were at least 12 months age the last time we observed them:
	keep if age_pikin >= 365

	* Drop babies born in December 2017 and January 2018:
	keep if dob < td(01dec2017)

	gen  measles1_350 = measles1
	gen  measles1_365 = measles1


	* ----------------------------------------------------------------------------
	* Create a follow-up indicator for each vaccine based on the age of the child
	*   during the last listing:

	local    VaccineVars = " bcg penta1 penta2 penta3 measles1 measles1_350 measles1_365 "

	foreach      vaccine of local VaccineVars {
		gen     age_`vaccine'_mopup    = .
	}

	replace age_bcg_mopup      = 0            if age_listing !=. & age_listing >= 91
	replace age_bcg_mopup      = 1            if age_listing !=. & age_listing <  91

	replace age_penta1_mopup   = 0            if age_listing !=. & age_listing >= 122
	replace age_penta1_mopup   = 1            if age_listing !=. & age_listing <  122

	replace age_penta2_mopup   = 0            if age_listing !=. & age_listing >= 152
	replace age_penta2_mopup   = 1            if age_listing !=. & age_listing <  152

	replace age_penta3_mopup   = 0            if age_listing !=. & age_listing >= 183
	replace age_penta3_mopup   = 1            if age_listing !=. & age_listing <  183


	foreach  num of numlist 350 365 {
	replace age_measles1_`num'_mopup = 0        if age_listing !=. & age_listing >= 365
	replace age_measles1_`num'_mopup = 1        if age_listing !=. & age_listing <  365
	}

	gen     age_num_vaccine_mopup = age_measles1_350_mopup

	* ------------------------------------------------------------------------------
	* Construct outcomes according to timeliness

	* Save the vacine outcomes in seperate variables that will be used to code
	*      the completeness outcome:
	foreach vaccine in bcg penta1 penta2 penta3 measles1 {
	gen `vaccine'_raw = `vaccine'
	}

	* Code Vaccine 1 outcome
	local a = 91

	foreach var in bcg {
	replace `var' = 0          if `var'==1 & age_`var'>=`a' & age_`var'!=.
	replace `var' = 0          if `var'==. & age_pikin>=`a' & age_pikin!=.
	replace `var' = .          if age_pikin<`a'
	}

	* Code Vaccine 2 outcome
	local b = 122

	foreach var in penta1 {
	replace `var' = 0          if `var'==1 & age_`var'>=`b' & age_`var'!=.
	replace `var' = 0          if `var'==. & age_pikin>=`b' & age_pikin!=.
	replace `var' = .          if age_pikin<`b'

	}

	* Code Vaccine 3 outcome
	local c = 152

	foreach  var in penta2 {
	replace `var' = 0          if `var'==1 & age_`var'>=`c' & age_`var'!=.
	replace `var' = 0          if `var'==. & age_pikin>=`c' & age_pikin!=.
	replace `var' = .          if age_pikin<`c'

	}

	* Code Vaccine 4 outcome
	* 183 days - 6 months
	local d = 183

	foreach  var in penta3 {
	replace `var' = 0          if `var'==1 & age_`var'>=`d' & age_`var'!=.
	replace `var' = 0          if `var'==. & age_pikin>=`d' & age_pikin!=. 
	replace `var' = .          if age_pikin<`d'

	}

	* Code Vaccine 5 outcome
	* 350 days - 11.5 months
	local e = 350

	foreach var in measles1 {
	replace `var'_350 = 0          if `var'_350 ==1&age_`var'>=`e'&age_`var'!=.
	replace `var'_350 = 0          if `var'_350 ==.&age_pikin>=`e'&age_pikin!=.
	replace `var'_350 = .          if age_pikin<`e'

	}


	* total number of timely vaccines    
	gen     num_vaccine = .
	replace num_vaccine = 0          if bcg==0
	replace num_vaccine = 1          if bcg==1      & penta1==0
	replace num_vaccine = 2          if penta1==1   & penta2==0
	replace num_vaccine = 3          if penta2==1   & penta3==0
	replace num_vaccine = 4          if penta3==1   & measles1_350==0
	replace num_vaccine = 5          if measles1_350==1

	* ----------------------------------------------------------------------------
	* Demean control variables for this sample:

	replace good_memory = 2          if good_memory ==.
	tab     good_memory, gen(recall)

	tab     arm_anc, gen(anc)

	foreach var in  recall1 recall2 distance clinic_pop anc2 anc3 anc4 {
		sum  `var' if intervention_arm==1, detail
		gen  `var'_dm    = `var' - `r(mean)'
	}


	summ    bcg penta1 penta2 penta3  measles1_350 num_vaccine distance_dm

	label var distance_dm "Distance"



	* ******************************************************************************
	* Run regressions: Generate table
	* ******************************************************************************

	local ANC_dm      = " anc2_dm anc3_dm anc4_dm  "
	local Controls_dm = " recall1_dm recall2_dm distance_dm clinic_pop_dm"

	sum num_vaccine if intervention_arm==1
	scalar C_mean        = r(mean)
	
	eststo clear
	foreach  vaccine in bcg penta1 penta2 penta3  measles1_350 num_vaccine {
		areg    `vaccine'  treat3 treat4 treat2  age_`vaccine'_mopup `Controls_dm'  `ANC_dm', absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
		test   treat2 = treat3
		estadd scalar treat2_treat3 = r(p)
		test   treat2 = treat4
		estadd scalar treat2_treat4 = r(p)
		test   treat3 = treat4
		estadd scalar treat3_treat4 = r(p)
		test   treat2 treat3 treat4
		estadd scalar bracelets     = r(p)
		estadd local Obs            = "`e(N)'"
		estadd scalar C_mean        = _b[_cons]
		estadd local controls     "Yes"
		eststo model_`vaccine'
	}

	esttab  ///
	model_bcg     model_penta1        model_penta2       model_penta3       ///
	model_measles1_350  model_num_vaccine  ///
	using "${Replicate_SocialSignals_ATables}/Table_EffectsOfTimely_raw.tex", ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule"                  ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{1}{c}{\textbf{Vaccine 1}} & \multicolumn{1}{c}{\textbf{Vaccine 2}} & \multicolumn{1}{c}{\textbf{Vaccine 3}} & \multicolumn{1}{c}{\textbf{Vaccine 4}} & \multicolumn{1}{c}{\textbf{Vaccine 5}} &  \multicolumn{1}{c}{\textbf{Total \# of vaccines}} \\")  ///
	scalars("C_mean         Control Group mean"                           ///
	"Obs            Observations"                                 ///
	"treat2_treat3  $S_{4} > 0$: p(UI = S4)"                ///
	"treat2_treat4  $S_{5} > 0$: p(UI = S5)"                ///
	"treat3_treat4  p(S4 = S5)"                                   ///
	"bracelets      Joint F-Test"                                 ///
	"controls       Controls")                                    ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	keep(_cons treat3 treat4 treat2 distance_dm)                          ///
	varlabels(_cons "Control Group mean")                                 ///
	sfmt(3) ${StarsOpt}   nomtitles                                       ///
	addnotes("") label b(3) se(3)  nobaselevels noconstant noobs          ///
	nolines posthead(\midrule) postfoot("\bottomrule \end{tabular}") tex  ///
	replace

	filefilter "${Replicate_SocialSignals_ATables}/Table_EffectsOfTimely_raw.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_EffectsOfTimely_NonAbsorbing.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_EffectsOfTimely_raw.tex"



	* ----------------------------------------------------------------------------
	* outsheet p-values, with controls


	esttab  ///
	model_bcg     model_penta1        model_penta2       model_penta3       ///
	model_measles1_350  model_num_vaccine  ///
	using "${Replicate_SocialSignals_ATables}/Table_EffectsOfTimely_raw.tex", ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule"                  ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{1}{c}{\textbf{Vaccine 1}} & \multicolumn{1}{c}{\textbf{Vaccine 2}} & \multicolumn{1}{c}{\textbf{Vaccine 3}} & \multicolumn{1}{c}{\textbf{Vaccine 4}} & \multicolumn{1}{c}{\textbf{Vaccine 5}} &  \multicolumn{1}{c}{\textbf{Total \# of vaccines}} \\")  ///
	scalars("C_mean         Control Group mean"                           ///
	"Obs            Observations"                                 ///
	"treat2_treat3  $S_{4} > 0$: p(UI = S4)"                ///
	"treat2_treat4  $S_{5} > 0$: p(UI = S5)"                ///
	"treat3_treat4  p(S4 = S5)"                                   ///
	"bracelets      Joint F-Test"                                 ///
	"controls       Controls")                                    ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	keep(_cons treat3 treat4 treat2 distance_dm)                          ///
	varlabels(_cons "Control Group mean")                                 ///
	sfmt(3) ${StarsOpt}   nomtitles                                       ///
	addnotes("") label b(3) p(3)  nobaselevels noconstant noobs          ///
	nolines posthead(\midrule) postfoot("\bottomrule \end{tabular}") tex  ///
	replace

	filefilter "${Replicate_SocialSignals_ATables}/Table_EffectsOfTimely_raw.tex"      ///
	"${Replicate_SocialSignals_ATables}/pvalues/Table_EffectsOfTimely_NonAbsorbing_pvals.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_EffectsOfTimely_raw.tex"





* ******************************************************************************
* End of the Dofile !!!
* ******************************************************************************
